#pragma once

#include <gameboycore/gameboycore.h>
#include <QThread>
#include <memory>

class CoreCaller
{
public:
	class Worker : public QThread
	{
	public:
		Worker(gb::GameboyCore& core) : m_core(core) {}

		void run()
		{
			m_core.emulateFrame();
		}

	private:
		gb::GameboyCore& m_core;
	};

	CoreCaller(gb::GameboyCore& core) : m_core(core)
	{}

	void start()
	{
		m_worker.reset(new Worker(m_core));
		m_worker->start();
	}

	void stop()
	{
		if (m_worker) {
			m_worker->exit();
			m_worker->wait();
		}
	}

	bool isRunning()
	{
		return m_worker && m_worker->isRunning();
	}

	~CoreCaller() = default;

	Worker* current_worker()
	{
		if (m_worker)
			return m_worker.get();
		
		return nullptr;
	}

private:
	gb::GameboyCore& m_core;
	std::unique_ptr<Worker> m_worker{ nullptr };
};